// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

namespace SnapObjects.Data
{
    internal abstract class SqlQueryStatementParser : SqlStatementParser
    {
        public SqlQueryStatementParser(
            ISqlStatementFactory factory, ISqlGenerationHelper helper) : base(factory, helper)
        {

        }

        protected virtual bool ParseFrom(ISqlTokenizer tokenizer, SqlQueryStatement statement)
        {
            var result = false;

            if (tokenizer.Current.IsKeyword(SqlKeywords.FROM))
            {
                while (tokenizer.MoveNext())
                {
                    if (tokenizer.Current is SqlKeywordToken key)
                    {
                        if (key.Keyword == SqlKeywords.SELECT)
                        {
                            var subStmt = this.ParseClause(tokenizer, statement.From);

                            statement.From.Add(subStmt);

                            if (tokenizer.Current == null)
                            {
                                break;
                            }
                        }
                        else if (key.Keyword == SqlKeywords.WHERE
                            || key.Keyword == SqlKeywords.GROUP
                            || key.Keyword == SqlKeywords.HAVING
                            || key.Keyword == SqlKeywords.ORDER
                            || key.Keyword == SqlKeywords.UNION)
                        {
                            result = true;

                            break;
                        }
                    }

                    if (this.IsStatementBracketEnd(tokenizer))
                    {
                        break;
                    }
                    else
                    {
                        statement.From.Add(tokenizer.Current);
                    }
                }
            }

            return result;
        }

        protected virtual bool ParseWhere(ISqlTokenizer tokenizer, SqlQueryStatement statement)
        {
            var result = false;

            if (tokenizer.Current.IsKeyword(SqlKeywords.WHERE))
            {
                while (tokenizer.MoveNext())
                {
                    if (tokenizer.Current is SqlKeywordToken key)
                    {
                        if (key.Keyword == SqlKeywords.SELECT)
                        {
                            var subStmt = this.ParseClause(tokenizer, statement.Where);

                            statement.Where.Add(subStmt);

                            if (tokenizer.Current == null)
                            {
                                break;
                            }
                        }
                        else if (key.Keyword == SqlKeywords.GROUP
                            || key.Keyword == SqlKeywords.HAVING
                            || key.Keyword == SqlKeywords.ORDER
                            || key.Keyword == SqlKeywords.UNION)
                        {
                            result = true;

                            break;
                        }
                    }

                    if (this.IsStatementBracketEnd(tokenizer))
                    {
                        break;
                    }
                    else
                    {
                        statement.Where.Add(tokenizer.Current);
                    }
                }
            }

            return result;
        }
    }
}
